package com.shop.manager.controller;


//import com.shop.manager.entity.SeckillOrder;
//import com.shop.manager.http.HttpResult;
//import com.shop.manager.http.PageResult;
import com.base.common.entity.SeckillOrder;
import com.base.common.http.HttpResult;
import com.base.common.http.PageResult;
import com.shop.manager.service.SecKillOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description 秒杀订单增删改查
 */
@RestController
@RequestMapping("/seckillOrder")
public class SecKillOrderController {

    /**
     * @Description 注入service对象
     */
    @Autowired
    private SecKillOrderService secKillOrderService;

    /**
     * @Description 添加秒杀订单
     * @param seckillOrder 秒杀订单对象
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    HttpResult add(@RequestBody(required = false) SeckillOrder seckillOrder){
        try {
            secKillOrderService.add(seckillOrder);
            return HttpResult.ok("添加秒杀订单成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return HttpResult.error("添加秒杀订单失败!");
    }

    /**
     * @Description 批量删除秒杀订单
     * @param ids 秒杀订单id的数组
     * @return
     */
    @RequestMapping("/delete")
    HttpResult delete(@RequestBody Long[] ids){
        try {
            secKillOrderService.deleteByIds(ids);
            //删除成功
            return HttpResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            //删除异常
            return HttpResult.error();
        }
    }

    /**
     * @Description 修改秒杀订单
     * @param seckillOrder 修改的秒杀订单对象
     * @return
     */
    @RequestMapping("/update")
    HttpResult update(@RequestBody(required = false) SeckillOrder seckillOrder){
        try {
            secKillOrderService.update(seckillOrder);
            return HttpResult.ok("修改秒杀订单成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return HttpResult.error("修改秒杀订单失败!");
    }

    /**
     * @Description 根据ID查询秒杀订单
     * @param id 秒杀订单id
     * @return
     */
    @RequestMapping("/getById")
    HttpResult getById(@RequestParam("id") Long id){
        return HttpResult.ok(secKillOrderService.findOne(id));
    }

    /**
     * @Description 条件分页查询
     * @param pageNum 当前页码
     * @param size 当前页条数
     * @param seckillOrder 查询条件对象
     * @return
     */
    @RequestMapping("/getAll")
    HttpResult getAll(@RequestParam("pageNum") Integer pageNum, @RequestParam("size") Integer size, @RequestBody(required = false) SeckillOrder seckillOrder){
        try {
            //分页查询
            PageResult result = secKillOrderService.findPage(pageNum, size, seckillOrder);
            //查询成功
            return HttpResult.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            //查询异常
            return HttpResult.error("查询秒杀订单异常");
        }
    }


}
